package com.boss;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ClassName TestCDL
 * @Description
 * @Author xinggh
 * @Date 2020/8/16 22:21
 * @Version 1.0
 **/
public class TestCDL {
    static CountDownLatch countDownLatch = new CountDownLatch(2);
    static CyclicBarrier cyclicBarrier = new CyclicBarrier(2);

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(2);

        executorService.submit(() -> {
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                System.out.println("Thread-1 执行完毕");
                countDownLatch.countDown();
            }
        });
        executorService.submit(() -> {
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                System.out.println("Thread-2 执行完毕");
                countDownLatch.countDown();
            }
        });

        System.out.println("开始前" + countDownLatch.getCount());
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("开始后" + countDownLatch.getCount());

    }
}
